package com.wrb.pwd.component;

import com.wrb.pwd.model.User;
import com.wrb.pwd.service.UserService;
import com.wrb.pwd.utils.MD5Utils;

import javax.swing.*;
import java.awt.*;
import java.util.Date;
import java.util.Random;

/**
 * @author wrb
 * @date 2019/9/17 14:01
 */
public class UserRecordPanel extends JPanel {

	private UserService userService = UserService.getInstance();

	private JLabel lbPlatform, lbName, lblPwd, lbPhone, lbEmail, lbAuxEmail, lbRemark, lbIMsg;

	//声明文本框
	private JTextField txtPlatform, txtName, txtPwd, txtEmail, txtPhone, txtAuxEmail, txtRemark;
	private JButton btnReg, btnCancel, btnGenerate;

	private String checkRepeat = "";

	private UserRecordPanel() {
		super(null);

		//实例化5个标签
		lbPlatform = new JLabel("平   台");
		lbName = new JLabel("用户名");
		lblPwd = new JLabel("密   码");
		lbPhone = new JLabel("手机号码");
		lbEmail = new JLabel("邮   箱");
		lbAuxEmail = new JLabel("辅助邮箱");
		lbRemark = new JLabel("备   注");

		//显示信息的标签
		lbIMsg = new JLabel();
		//设置标签的文字是红色
		lbIMsg.setForeground(Color.RED);

		txtPlatform = new JTextField(20);
		txtName = new JTextField(20);
		txtPwd = new JTextField(20);
		txtPwd.setEditable(false);
		txtPhone = new JTextField(20);
		txtEmail = new JTextField(20);
		txtAuxEmail = new JTextField(20);
		txtRemark = new JTextField(20);

		//创建两个按钮
		btnReg = new JButton("记录");
		btnCancel = new JButton("清空");
		btnGenerate = new JButton("生成密码");

		//生成密码
		btnGenerate.addActionListener(e -> {
			//设置标签的文字是红色
			lbIMsg.setText("");
			lbIMsg.setForeground(Color.RED);

			String platformStr = txtPlatform.getText();
			if (platformStr == null || "".equals(platformStr.trim())) {
				lbIMsg.setText("平台不能为空");
				return;
			}
			String password = MD5Utils.md5(platformStr + MD5Utils.salt);
			String pwdStr = password.substring(0, 15);
			boolean isEdit = false;
			for (int i = 0; i < pwdStr.length(); i++) {
				char c = pwdStr.charAt(i);//获取当前char字符
				if (Character.isUpperCase(c)) {//如果是大写
					//把大写改为小写
					pwdStr = pwdStr.replaceFirst(String.valueOf(c), (char) (c + 32) + "!");
					isEdit = true;
					break;
				}
			}
			if (!isEdit) {
				int rank = new Random(platformStr.charAt(0)).nextInt(10) + 1;
				StringBuilder sb = new StringBuilder(pwdStr);
				int c = 'a' + new Random(rank).nextInt(26);
				sb.replace(rank, rank + 1, (char) c + "!");
				pwdStr = sb.toString();
			}
			txtPwd.setText(pwdStr);
			lbIMsg.setForeground(Color.GREEN);
			lbIMsg.setText("生成密码成功");
		});

		//注册监听
		btnReg.addActionListener(e -> {
			//设置信息标签为空 清楚原来的历史信息
			lbIMsg.setText("");
			//设置标签的文字是红色
			lbIMsg.setForeground(Color.RED);

			//获取平台
			String platformStr = txtPlatform.getText();
			if (platformStr == null || "".equals(platformStr.trim())) {
				lbIMsg.setText("平台不能为空");
				return;
			}
			//获取用户输入的用户名
			String nameStr = txtName.getText();
			if (nameStr == null || "".equals(nameStr.trim())) {
				lbIMsg.setText("用户名不能为空");
				return;
			}
			String pwdStr = txtPwd.getText();
			if (pwdStr == null || "".equals(pwdStr.trim())) {
				lbIMsg.setText("密码不能为空");
				return;
			}
			User user = new User();
			user.setId(String.valueOf(System.currentTimeMillis()));
			user.setPlatform(platformStr);
			user.setName(nameStr);
			user.setPassword(pwdStr);
			String phoneStr = txtPhone.getText();
			if (phoneStr != null && !"".equals(phoneStr.trim())) {
				user.setPhone(phoneStr);
			}
			String emailStr = txtEmail.getText();
			if (emailStr != null && !"".equals(emailStr.trim())) {
				user.setEmail(emailStr);
			}
			String auxiliaryEmailStr = txtAuxEmail.getText();
			if (auxiliaryEmailStr != null && !"".equals(auxiliaryEmailStr.trim())) {
				user.setAuxiliaryEmail(auxiliaryEmailStr);
			}
			String remarkStr = txtRemark.getText();
			if (remarkStr != null && !"".equals(remarkStr.trim())) {
				user.setRemark(remarkStr);
			}
			if (checkRepeat.equals(platformStr + nameStr)) {
				lbIMsg.setText("请勿重复记录");
				return;
			} else {
				checkRepeat = platformStr + nameStr;
			}
			Date now = new Date();
			user.setCreateTime(now);
			user.setUpdateTime(now);

			boolean isSuccess = userService.insertUser(user);
			if (isSuccess) {
				lbIMsg.setForeground(Color.GREEN);
				lbIMsg.setText("记录成功");
			} else {
				lbIMsg.setText("记录失败");
			}


		});

		//清空按钮的事件处理
		btnCancel.addActionListener(e -> {
			this.init();
		});

		lbPlatform.setBounds(30, 30, 60, 25);
		txtPlatform.setBounds(95, 30, 180, 25);
		lbName.setBounds(30, 60, 60, 25);
		txtName.setBounds(95, 60, 180, 25);
		lblPwd.setBounds(30, 90, 60, 25);
		txtPwd.setBounds(95, 90, 180, 25);
		btnGenerate.setBounds(280, 90, 120, 25);
		lbPhone.setBounds(30, 120, 60, 25);
		txtPhone.setBounds(95, 120, 180, 25);
		lbEmail.setBounds(30, 150, 60, 25);
		txtEmail.setBounds(95, 150, 180, 25);
		lbAuxEmail.setBounds(30, 180, 60, 25);
		txtAuxEmail.setBounds(95, 180, 180, 25);
		lbRemark.setBounds(30, 210, 60, 25);
		txtRemark.setBounds(95, 210, 180, 25);
		lbIMsg.setBounds(60, 240, 180, 25);
		btnReg.setBounds(60, 270, 60, 25);
		btnCancel.setBounds(130, 270, 60, 25);

		//添加所有组件
		this.add(lbPlatform);
		this.add(txtPlatform);
		this.add(lbName);
		this.add(txtName);
		this.add(lblPwd);
		this.add(txtPwd);
		this.add(btnGenerate);
		this.add(lbPhone);
		this.add(txtPhone);
		this.add(lbEmail);
		this.add(txtEmail);
		this.add(lbAuxEmail);
		this.add(txtAuxEmail);
		this.add(lbRemark);
		this.add(txtRemark);
		this.add(lbIMsg);
		this.add(btnReg);
		this.add(btnCancel);
	}

	public void setHomeButton(JButton jbHome) {
		jbHome.setBounds(200, 270, 60, 25);
		this.add(jbHome);
	}

	public void init() {
		this.txtPlatform.setText("");
		this.txtName.setText("");
		this.txtPwd.setText("");
		this.txtPhone.setText("");
		this.txtEmail.setText("");
		this.txtAuxEmail.setText("");
		this.txtRemark.setText("");
		this.lbIMsg.setText("");
	}

	public static UserRecordPanel getInstance() {
		return Inner.INSTANCE;
	}

	private static class Inner {
		private static final UserRecordPanel INSTANCE = new UserRecordPanel();
	}

}
